package vns.client.view;

import java.util.ArrayList;
import java.util.List;

import vns.client.helper.Helper;
import vns.client.presenter.RolePresenter;
import vns.shared.RoleDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLPanel;

public class RoleUI extends Composite implements RolePresenter.Display {

	private List<String> listStringColumn = new ArrayList<String>();
	private static int rowSelectedID;
	private List<RoleDTO> listRow = new ArrayList<RoleDTO>();

	private static ObjectPropertyTypeUIUiBinder uiBinder = GWT
			.create(ObjectPropertyTypeUIUiBinder.class);
	@UiField
	FlexTable flexTable;
	@UiField
	HTMLPanel htmlPanelRole;
	@UiField
	HTMLPanel htmlpanelRoleDetail;
	@UiField
	Button btAdd;
	@UiField
	Button btDel;
	@UiField
	Button btUpdate;
	@UiField
	Button btEdit;

	interface ObjectPropertyTypeUIUiBinder extends UiBinder<Widget, RoleUI> {
	}

	/*
	 * Constructor
	 */
	public RoleUI() {
		initWidget(uiBinder.createAndBindUi(this));
		bind();
	}

	private void bind() {
		/*
		 * TÃªn cÃ¡c cá»™t
		 */
		listStringColumn.add(""); // order
		listStringColumn.add("TÃªn vai trÃ²/chá»©c vá»¥"); // name
		listStringColumn.add("MÃ´ táº£"); // des

		flexTable.setBorderWidth(1);
		flexTable.setCellPadding(2);
		flexTable.setCellSpacing(0);

		/*
		 * FlexTable Even
		 */
		flexTable.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				getClickedRow(event);

			}
		});
	}

	/*************************************************************************************/
	public void renderClickedRow() {
		if (rowSelectedID > 0) {
			HTMLTable.RowFormatter rf = flexTable.getRowFormatter();
			rf.addStyleName(rowSelectedID, "vnsClickedRow");
		}
	}

	public void unrenderClickedRow() {
		if (rowSelectedID > 0) {
			HTMLTable.RowFormatter rf = flexTable.getRowFormatter();
			rf.removeStyleName(rowSelectedID, "vnsClickedRow");
		}
	}

	public int getClickedRow(ClickEvent event) {
		int selectedRow = -1;
		HTMLTable.Cell cell = flexTable.getCellForEvent(event);

		if (cell != null) {
			if (cell.getCellIndex() >= 0) {
				selectedRow = cell.getRowIndex();
			}
		}
		swapClickedRow(selectedRow, cell.getCellIndex());
		return selectedRow;
	}

	private void swapClickedRow(int rowNow, int cellindex) {
		if (rowNow != rowSelectedID && rowNow > 0) {
			unrenderClickedRow();
			setRowEnable(rowSelectedID, false);
			rowSelectedID = rowNow;

			renderClickedRow();
			setRowEnable(rowSelectedID, true);
		} else if (rowNow == rowSelectedID && rowNow > 0) {
			if (cellindex == 0) {
				unrenderClickedRow();
				setRowEnable(rowSelectedID, false);
				rowSelectedID = -1;
			}
		}
	}

	/*
	 * Set Row Enable
	 */
	private void setRowEnable(int row, boolean value) {
		if (row <= flexTable.getRowCount() - 1 && row > 0) {
			CheckBox orderCheckBox = (CheckBox) flexTable.getWidget(row, 0);
			orderCheckBox.setValue(value);
			TextBox nameTextBox = (TextBox) flexTable.getWidget(row, 1);
			nameTextBox.setReadOnly(!value);
			TextBox desTextBox = (TextBox) flexTable.getWidget(row, 2);
			desTextBox.setReadOnly(!value);
		}
	}

	/*
	 * Create Order CheckBox
	 */
	private CheckBox OrderCheckBox(int row, boolean value, boolean enable) {
		final CheckBox cb = new CheckBox();
		cb.setValue(value);
		cb.setEnabled(enable);
		return cb;
	}

	/********************************************************************************/
	/*
	 * Init
	 */

	private void initTableFirst() {
		flexTable.removeAllRows();
		/*
		 * TÃªn cÃ¡c cá»™t
		 */
		int i = 0;
		for (final String columnName : listStringColumn) {
			flexTable.setHTML(0, i, "<center><b>" + columnName
					+ "</b></center>");
			i++;
		}
	}

	/*
	 * Add Row with Wiget
	 */
	private int addRow(Widget nameTextbox, Widget desTextbox) {
		int row = flexTable.getRowCount();
		CheckBox sttCheckbox = OrderCheckBox(row, false, true);
		flexTable.setWidget(row, 0, sttCheckbox);
		flexTable.setWidget(row, 1, nameTextbox);
		flexTable.setWidget(row, 2, desTextbox);
		setRowEnable(row, false);
		return row;
	}

	/*
	 * Add Row with data
	 */
	private int addRow(String name, String des) {
		int row = flexTable.getRowCount();

		CheckBox sttCheckbox = OrderCheckBox(row, false, true);
		flexTable.setWidget(row, 0, sttCheckbox);

		TextBox nameTextBox = new TextBox();
		nameTextBox.setValue(name);
		flexTable.setWidget(row, 1, nameTextBox);

		TextBox desTextBox = new TextBox();
		desTextBox.setValue(des);
		flexTable.setWidget(row, 2, desTextBox);

		setRowEnable(row, false);

		return row;
	}

	/*
	 * Add Row Empty
	 */
	private int addRow() {
		int row = addRow(new TextBox(), new TextBox());
		setRowEnable(row, false);
		return row;
	}

	/*
	 * Remove selected Row
	 */
	private void removeRow() {
		if (rowSelectedID > 0) {
			flexTable.removeRow(rowSelectedID);
			int remvIndex = Helper.getIndexByRow(rowSelectedID, listRow.size());
			if (remvIndex != -1) {
				listRow.remove(remvIndex);
			}
			rowSelectedID = -1;
		} else if (rowSelectedID == -1)
			Window.alert("Chọn hàng cần xóa!");
		else
			Window.alert("Không xóa kiểu cơ bản!");
	}

	/*
	 * Update all rows
	 */
	private void updateRow() {
		for (int i = 1; i < listRow.size() ; i++) {
			int index = Helper.getIndexByRow(i, listRow.size());

			TextBox nameTextBox = (TextBox) flexTable.getWidget(i, 1);
			TextBox desTextBox = (TextBox) flexTable.getWidget(i, 2);
			listRow.get(index).setName(nameTextBox.getValue());
			listRow.get(index).setDescribe(desTextBox.getValue());
		}

		if (flexTable.getRowCount() - 1 > listRow.size()) {
			for (int row = listRow.size()+1; row < flexTable
					.getRowCount(); row++) {
				String name = ((TextBox) flexTable.getWidget(row, 1)).getValue();
				String des = ((TextBox) flexTable.getWidget(row, 2)).getValue();
				RoleDTO o = new RoleDTO(null, name, des);
				listRow.add(o);
			}
		}
	}
	
	/*
	 * Event
	 */

	@UiHandler("btAdd")
	void onBtAddClick(ClickEvent event) {
		addRow();
	}

	 @UiHandler("btUpdate")
	 void onBtEditClick(ClickEvent event) {
		 updateRow();
	 }

	@UiHandler("btDel")
	void onBtDelClick(ClickEvent event) {
		removeRow();
	}

	/*********************************************************/

	@Override
	public Button getUpdateButton() {
		return btUpdate;
	}

	@Override
	public Button getEditButton() {
		return btEdit;
	}

	@Override
	public List<RoleDTO> getListRow() {
		return (List<RoleDTO>) listRow;
	}

	@Override
	public boolean setListRow(List<RoleDTO> listRow) {
		this.listRow = listRow;
		return true;
	}

	@Override
	public void render() {
		rowSelectedID = -1;
		initTableFirst();
		for (final RoleDTO o : this.listRow) {
			addRow(o.getName(), o.getDescribe());
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}
}
